package com.example.apple.tianqiyubaoone;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.provider.CalendarContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.apple.tianqiyubaoone.DATA.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import me.tangke.slidemenu.SlideMenu;

public class MainActivity extends Activity{
    TextView 城市,切换,温度,今日天气,今日气象,今日温度;
    TextView 当前温度,当前湿度,当前风力,更新时间,当前风向;
    Button 未来七日的天气;
    SharedPreferences preferences;
    GridView gridView;
    SimpleAdapter myAdapter;
    private ArrayList<Map<String,Object>> data_list;
    public static MainActivity maina=null;
    private int[] icon={R.drawable.zhiwaixianqiandu,R.drawable.washbus,R.drawable.lvyou,R.drawable.chengliangzhishu,R.drawable.zhishu,R.drawable.jianyi,};
    private String[] iconName = { "紫外线强度", "洗车指数", "旅游指数", "晨练指数","穿衣指数", "穿衣建议",};
    private String[] text;
    static CalendarContract.Instances instances;
    Request request = new Request();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SlideMenu slideMenu =new SlideMenu(this);
        setContentView(slideMenu);
        // 设置主菜单
        View contentView = View.inflate(getWindow().getContext(),R.layout.activity_main,null);
        slideMenu.addView(contentView, new SlideMenu.LayoutParams(
                SlideMenu.LayoutParams.MATCH_PARENT, SlideMenu.LayoutParams.MATCH_PARENT,
                SlideMenu.LayoutParams.ROLE_CONTENT));
        // 设置一级菜单
        View primaryMenu =View.inflate(MainActivity.this,R.layout.activity_main_primarymenu,null);
        slideMenu.addView(primaryMenu, new SlideMenu.LayoutParams(600,
                SlideMenu.LayoutParams.MATCH_PARENT, SlideMenu.LayoutParams.ROLE_PRIMARY_MENU));
//        //设置二级菜单
//        View secondaryMenu = new View(this);
//        slideMenu.addView(secondaryMenu, new SlideMenu.LayoutParams(300,
//                SlideMenu.LayoutParams.MATCH_PARENT, SlideMenu.LayoutParams.ROLE_SECONDARY_MENU));
        //woyunlkao
        maina =this;
        gridView =(GridView)contentView.findViewById(R.id.gridView);
        城市 = (TextView)contentView.findViewById(R.id.城市);
        切换 = (TextView) contentView.findViewById(R.id.切换);
        温度 = (TextView)contentView.findViewById(R.id.温度);
        今日天气 = (TextView)contentView.findViewById(R.id.今日天气);
        今日气象 = (TextView)contentView.findViewById(R.id.今日气象);
        今日温度 = (TextView)contentView.findViewById(R.id.今日温度);
        当前温度 = (TextView)contentView.findViewById(R.id.当前温度);
        当前湿度 = (TextView)contentView.findViewById(R.id.当前湿度);
        当前风力 = (TextView)contentView.findViewById(R.id.当前风力);
        更新时间 = (TextView)contentView.findViewById(R.id.更新时间);
        当前风向 = (TextView)contentView.findViewById(R.id.当前风向);
        切换.setOnClickListener(new OnClickSwitch());
        未来七日的天气 =(Button)contentView.findViewById(R.id.七天的天气);
        未来七日的天气.setOnClickListener(new OnClickSwitch());
        preferences =getSharedPreferences("met",Activity.MODE_PRIVATE);
        城市.setText(preferences.getString("city",""));
        if (城市.getText()==""){
            城市.setText("哈尔滨");
        }
        setData();
    }
    private void setData() {
        //Log.e("网络－－>", String.valueOf(request.isNetworkConnected(getWindow().getContext())));
        if (!request.isNetworkConnected(getWindow().getContext())){
            Toast.makeText(getWindow().getContext(),"无网络",Toast.LENGTH_SHORT).show();
            return;
        }
        request.doGet("http://v.juhe.cn/weather/index","?cityname="+城市.getText()+"&key=91ce84d59482d9ee8fd766a56afdf284",new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                ParsingData.setParsingData(msg);
               refreshData();
              //  Log.e("解析数据－－－>", Data.createSingletonData().getResult().getSk().getTime().toString());
            }
        });
    }
    private void refreshData(){
        Data data = Data.createSingletonData();
        Log.e("-->",data.toString());
        温度.setText(data.getResult().getSk().getTemp());
        今日天气.setText(data.getResult().getToday().getWeatherData().getWeather());
        今日气象.setText(data.getResult().getSk().getWind_direction());
        今日温度.setText(data.getResult().getToday().getWeatherData().getTemperature());
        当前温度.setText(data.getResult().getSk().getTemp());
        当前湿度.setText(data.getResult().getSk().getHumidity());
        当前风力.setText(data.getResult().getSk().getWind_strength());
        当前风向.setText(data.getResult().getSk().getWind_direction());
        更新时间.setText(data.getResult().getSk().getTime());
        text = new String[]{data.getResult().getToday().getUv_index(),data.getResult().getToday().getWash_index(),data.getResult().getToday().getTravel_index(),data.getResult().getToday().getExercise_index(),data.getResult().getToday().getDressing_index(),data.getResult().getToday().getDressing_advice()};
        getData();
        String [] from ={"image","title","text"};
        int [] to = {R.id.icon,R.id.iconname,R.id.text};
        myAdapter = new SimpleAdapter(this,data_list,R.layout.main_item,from,to);
        gridView.setAdapter(myAdapter);
    }
    public ArrayList<Map<String, Object>> getData(){
        data_list = new ArrayList<>();
        //cion和iconName的长度是相同的，这里任选其一都可以
        for(int i=0;i<icon.length;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("title", iconName[i]);
            map.put("text",text[i]);
            data_list.add(map);
        }

        return data_list;
    }

        private class OnClickSwitch implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.切换:
                    Intent intent = new Intent();
                    intent.setClass(getWindow().getContext(), SetCityeActivity.class);
                    startActivity(intent);
                    break;
                case R.id.七天的天气:
                    if (!request.isNetworkConnected(getWindow().getContext())){
                        Toast.makeText(getWindow().getContext(),"无网络",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Intent intent1= new Intent();
                    intent1.setClass(getWindow().getContext(),FutureSixDayActivity.class);
                    startActivity(intent1);
                    break;
            }
        }
    }

}
